<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property CI_DB_active_record $db
 */

class Post_model extends CI_Model
{

    public function getPostByCatId($catId, $numPost = '', $start = '', $langCode)
    {
        $return = array();
        $this->db->select("id, title_$langCode as 'title', description_$langCode as 'description', link_img, modified");
        $this->db->from('post');
        $this->db->where(array(
            'parent_id' =>  $catId,
            'status'    =>  1
        ));
        $this->db->order_by('modified', 'desc'); 
        if($numPost != '' || $start != '')
            $this->db->limit($numPost, $start);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getGoodPost($langCode)
    {
        $return = array();
        $this->db->select("id, title_$langCode as 'title', description_$langCode as 'description', link_img");
        $this->db->from('post');
        $this->db->where(array(
            'good_post' =>  1,
            'status'    =>  1
        ));
        $this->db->order_by('modified', 'desc'); 
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getNumPostByCatId($catId)
    {
        $this->db->from('post');
        $this->db->where(array(
            'parent_id' =>  $catId,
            'status'    =>  1
        ));
        return $this->db->count_all_results();
    }
    
    public function getInfoById($id, $langCode)
    {
        $return = array();
        $this->db->select("post.id, post.parent_id, post.title_$langCode as 'title', post.description_$langCode as 'description', post.content_$langCode as 'content', post.modified, post.link_img, category.name_$langCode as 'parent_name', modified");
        $this->db->from('post');
        $this->db->join('category', 'post.parent_id = category.id', 'inner');
        $this->db->where(array(
            'post.id'   =>  $id,
            'post.status'    =>  1
        ));
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        }
    }
    
    public function getMorePostWithType($postId, $parent, $langCode)
    {
        $return = array();
        $this->db->select("post.id, post.parent_id, post.title_$langCode as 'title', post.description_$langCode as 'description',category.name_$langCode as 'parent_name', post.modified, post.link_img");
        $this->db->from('post');
        $this->db->join('category', 'post.parent_id = category.id', 'inner');
        $this->db->where(array(
            'post.status' => 1,
            'category.id'   =>  $parent,
            'post.id !=' => $postId
        ));
        $this->db->order_by('modified', 'desc');
        $this->db->limit(5);
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        }
    }

}

?>
